package com.guxingke.gblog.controller.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.guxingke.gblog.controller.BaseController;
import com.guxingke.gblog.dmo.User;
import com.guxingke.gblog.dto.UserDto;
import com.guxingke.gblog.util.StringUtil;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

	@RequestMapping
	public String preLogin() {
		return "login";
	}

	@RequestMapping("/login")
	public String login(User user, HttpSession session,
			HttpServletRequest request) {

		if (StringUtil.isEmpty(user.getUsername())
				|| StringUtil.isEmpty(user.getPassword())) {
			request.setAttribute("error", "用户名或密码不能为空");
			return "login";
		}

		UserDto currentUser = userService.login(user);
		if (currentUser == null) {
			request.setAttribute("error", "用户名或密码错误");
			return "login";
		} else {
			session.setAttribute("currentUser", currentUser);
			if (currentUser.getState() != 0) {
				session.setAttribute("admin", currentUser);
				return "redirect:/manage.do";
			}
		}
		return "redirect:/home.do";
	}

	@RequestMapping("/quit")
	public String quit(HttpSession session) {
		session.invalidate();
		return "redirect:/home.do";
	}
}
